﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace Hangout.Client.BackgroundAgent
{
    public static class Settings
    {
        
        // Our isolated storage settings
        static IsolatedStorageSettings isolatedStore = IsolatedStorageSettings.ApplicationSettings;

        static  string UserIdKeyName = "UserID";
        static  string FbAccessTokenKeyName = "FBAccessToken";
        static  int UserIdDefault = -1;
        static  string FbAccessTokenDefault = "";

        

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(key))
            {
                // If the value has changed
                if (isolatedStore[key] != value)
                {
                    // Store the new value
                    isolatedStore[key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType)isolatedStore[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public static void Save()
        {
            isolatedStore.Save();
        }

        public static int UserID
        {
            get
            {
                return GetValueOrDefault<int>(UserIdKeyName, UserIdDefault);
            }
            
        }

        public static string FacebookAccessToken
        {
            get
            {
                return GetValueOrDefault<string>(FbAccessTokenKeyName, FbAccessTokenDefault);
            }
            set
            {
                AddOrUpdateValue(FbAccessTokenKeyName, value);
                Save();
            }
        }
    }
}
